package com.room.controller.visit;

import com.room.pojo.DormResult;
import com.room.pojo.Teachaer;
import com.room.pojo.Visit;
import com.room.pojo.vo.VisitVO;
import com.room.service.VisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/16
 * @描述
 */
@RestController
@RequestMapping("/visit/basic")
public class VisitController {

    @Autowired
    VisitService visitService;

    @RequestMapping(value = "/visitVO",method = RequestMethod.GET)
    public List<Visit> getVisitByName(@RequestParam  String name){
        if(name==null){
            return null;
        }
        return visitService.getVisitByRoom(name);
    }

    /**
     * 修改教师信息
     * @param visit
     * @return
     */
    @RequestMapping(value = "/visit",method = RequestMethod.PUT)
    public DormResult updateVisit(Visit visit){

        if(visitService.updateVisit(visit)==1){
            return DormResult.ok("修改成功");
        }
        return DormResult.error("修改失败");
    }

    /**
     * 删除指定id的访客
     * @param ids
     * @return
     */
    @RequestMapping(value = "/visit/{ids}",method = RequestMethod.DELETE)
    public DormResult delteByTeaId(@PathVariable String ids){
        if(visitService.deleteVisitById(ids)){
            return DormResult.ok("删除记录成功");
        }
        return DormResult.error("删除记录失败");
    }

    /**
     * 获得访客的分页数据
     * @param page
     * @param size
     * @return返回访客的分页数据
     */
    @RequestMapping(value = "/visit",method = RequestMethod.GET)
    public Map<String,Object> getVisitByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){

        Map<String,Object> map = new HashMap<String,Object>();
        List<Visit> visits = visitService.getVisitByPage(page,size);
        long total = visitService.getTotal();
        map.put("visits",visits);
        map.put("total",total);
        return map;
    }


    /**
     * 添加访客
     * @param visit
     * @return
     */
    @RequestMapping(value = "/visit",method = RequestMethod.POST)
    public DormResult addVisit(Visit visit){
     visit.setVisitTime(new Date());
        if(visitService.insertVisit(visit)==1){
            return DormResult.ok("添加成功");
        }
        return DormResult.error("添加失败");
    }
}
